﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using System.Web;

namespace yeqw.FrameWork
{
    public static class StringExtensions
    {

        public static string FormatExtension(this string input, params object[] args)
        {
            lock(typeof(StringExtensions)){
                return string.Format(input, args);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="parrtern"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string input)
        {
            lock (typeof(StringExtensions))
            {
                return string.IsNullOrEmpty(input);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="parrtern"></param>
        /// <returns></returns>
        public static bool Validate(this string input, string parrtern)
        {
            return Regex.IsMatch(input, parrtern);
        }
        /// <summary>
        /// 检查是否有数字和字母组成的
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsAZaz09(this string input)
        {
            return Regex.IsMatch(input, @"^[A-Za-z0-9]+$");
        }
        /// <summary>
        ///判断输入字符串是否纯英文.
        /// </summary>
        /// <param name="input">待输入测试字符串</param>
        /// <returns>bool 对于全部为英文的输入返回真，否则返回假</returns>
        public static bool IsEnglish(this string input)
        {
            return Regex.IsMatch(input, @"^[a-zA-Z]+$");
        }
        /// <summary>
        /// 检查是否为中文
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsChinese(this string input)
        {
            return (Regex.IsMatch(input, "^([\u4e00-\u9fa5]|[\ufe30-\uffa0])+$"));
        }
        /// <summary>
        /// 检查是否符合邮箱格式
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsEmail(this string input)
        {
            return (Regex.IsMatch(input, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"));
        }
        /// <summary>
        /// 判断输入字符串是否符合URL格式.
        /// </summary>
        /// <param name="input">待输入测试字符串</param>
        /// <returns>bool 对于合法的URL地址返回真，否则返回假</returns>
        public static bool IsUrl(this string input)
        {
            return Regex.IsMatch(input, @"^http:\/\/[A-Za-z0-9\./=\?%\-&_~`@[\]\':+!]+([^<>""])+$");
        }

        /// <summary>
        /// 判断输入字符串是否符合电话号码格式.
        /// </summary>
        /// <param name="input">待输入测试字符串</param>
        /// <returns>bool 对于合法的电话号码返回真，否则返回假</returns>
        public static bool IsPhone(this string input)
        {
            return Regex.IsMatch(input, @"^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$");
        }


        /// <summary>
        /// 判断是否有效的身份证号码。
        /// </summary>
        /// <param name="input">待验证的身份证号码</param>
        /// <returns>只有严格符合真实身份证的规则才为真</returns>
        public static bool IsIdCard(this string input)
        {
            string date, Ai;
            string verify = "10x98765432";
            int[] Wi = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            string[] area = new string[] { "", "", "", "", "", "", "", "", "", "", "", "北京", "天津", "河北", "山西", "内蒙古", "", "", "", "", "", "辽宁", "吉林", "黑龙江", "", "", "", "", "", "", "", "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", "", "", "", "河南", "湖北", "湖南", "广东", "广西", "海南", "", "", "", "重庆", "四川", "贵州", "云南", "西藏", "", "", "", "", "", "", "陕西", "甘肃", "青海", "宁夏", "新疆", "", "", "", "", "", "台湾", "", "", "", "", "", "", "", "", "", "香港", "澳门", "", "", "", "", "", "", "", "", "国外" };
            Match re = Regex.Match(input, @"^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[xX\d])))$");
            if (re.Length == 0) return false;
            if (int.Parse(re.Result("$1")) >= area.Length || area[int.Parse(re.Result("$1"))] == "") return false;
            if (re.Result("$2").Length == 12)
            {
                Ai = input.Substring(0, 17);
                date = string.Format("{0}-{1}-{2}", re.Result("$9"), re.Result("$10"), re.Result("$11"));
            }
            else
            {
                Ai = input.Substring(0, 6) + "19" + input.Substring(6);
                date = string.Format("19{0}-{1}-{2}", re.Result("$4"), re.Result("$5"), re.Result("$6"));
            }
            try
            {
                DateTime.Parse(date);
            }
            catch (Exception) { return false; }
            int sum = 0;
            for (int i = 0; i <= 16; i++)
            {
                sum += int.Parse(Ai.Substring(i, 1)) * Wi[i];
            }
            Ai += verify.Substring(sum % 11, 1);
            return (input.Length == 15 || input.Length == 18 && input == Ai);
        }

        /// <summary>
        /// 判断输入字符串是否符合IP格式.
        /// </summary>
        /// <param name="input">待输入测试字符串</param>
        /// <returns>bool 对于符合IP格式的返回真，否则返回假</returns>
        public static bool IsIP(this string input)
        {
            return Regex.IsMatch(input, @"^(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5])$");
        }

        /// <summary>
        /// 检查正负整数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsALLInteger(this string input)
        {
            return Regex.IsMatch(input, @"^([-]\d+)|\d+$");
        }
        /// <summary>
        /// 判断是否为BYTE类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsByte(this string input)
        {
            byte result;
            return byte.TryParse(input, out result);
        }
        /// <summary>
        /// 转为BYTE类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static int ToByte(this string input)
        {
            return byte.Parse(input);
        }
        /// <summary>
        /// 检查是否为正整数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsInteger(this string input)
        {
            return Regex.IsMatch(input, @"^[0-9]+$");
        }
        /// <summary>
        /// 转为整型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static long ToInt64(this string input)
        {
            lock (typeof(StringExtensions))
            {
                return long.Parse(input);
            }
        }

        /// <summary>
        /// 转为整型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static int ToInteger(this string input)
        {
            lock (typeof(StringExtensions))
            {
                return int.Parse(input);
            }
        }
        /// <summary>
        /// 检查是否为数字，带小数点
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsDecimal(this string input)
        {
            lock (typeof(StringExtensions))
            {
                decimal result;
                return decimal.TryParse(input, out result);
            }

        }
        /// <summary>
        /// 转为decimal
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this string input)
        {
            lock (typeof(StringExtensions))
            {
                return decimal.Parse(input);
            }
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="stringToSub">需要截取的字符串</param>
        /// <param name="length">截取长度</param>
        /// <param name="flage">超过长度显示的标记</param>
        /// <returns></returns>
        public static string SubStringCN(this string stringToSub, int length, string flage)
        {
            if (stringToSub.IsNullOrEmpty()) return string.Empty;
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            char[] stringChar = stringToSub.ToCharArray();

            int byteLen = System.Text.Encoding.Default.GetByteCount(stringToSub);
            StringBuilder sb = new StringBuilder();
            string retStr = string.Empty;
            int nLength = 0;

            for (int i = 0; i < byteLen; i++)
            {
                if (i >= stringChar.Length)
                    break;
                if (regex.IsMatch((stringChar[i]).ToString()))
                    nLength += 2;
                else
                    nLength = nLength + 1;
                sb.Append(stringChar[i]);

                if (nLength > length)
                {
                    retStr = sb.ToString(0, sb.ToString().Length - 1) + flage;
                    return retStr;
                }
                else
                    retStr = sb.ToString();
            }
            return retStr;
        }


        /// <summary>
        /// 去除HTML标记
        /// </summary>
        /// <param name="NoHTML">包括HTML的源码 </param>
        /// <returns>已经去除后的文字</returns>
        public static string NoHTML(this string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");
            Htmlstring = Htmlstring.Replace("&hellip;", "");
            Htmlstring = Htmlstring.Replace("&mdash;", "");
            Htmlstring = Htmlstring.Replace("&rdquo;", "");
            Htmlstring = Htmlstring.Replace("&ldquo;", "");
            //Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
            return Htmlstring;
        }

        public  static bool CheckSqlCondition(this string input)
        {
            return true;
        }

        /// <summary>
        /// JSONToDataTable
        /// </summary>
        /// <param name="input"></param>
        /// <param name="parrtern"></param>
        /// <returns></returns>
        public static T ToObject<T>(this string input) where T:class,new()
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            return json.Deserialize <T>(input);
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="base64Str"></param>
        /// <returns></returns>
        public static object Deserialize(this string base64Str)
        {
            return SerializeHelper.Deserialize(base64Str);
        }
       
    }
}
